package com.fire.social.controller;

import com.fire.social.entity.node.User;
import com.fire.social.mapper.FollowMapper;
import com.fire.social.service.UserService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Map;

@Controller
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private FollowMapper followMapper;

    @GetMapping("/")
    public String index(Map<String, Object> paramMap, HttpServletRequest request) {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return "login";
        } else {
            return "redirect:/content";
        }
    }

    @PostMapping("/login")
    public String index(@RequestParam("account") String account,
                        @RequestParam("password") String password,
                        Map<String, Object> paramMap,
                        HttpServletRequest request) {
        User user = userService.loginUser(account, password);
        if (user == null) {
            paramMap.put("msg", "用户名或者密码错误");
            return "login";
        } else {
            HttpSession session = request.getSession();
            session.setAttribute("user", user);
            return "redirect:content";
        }
    }

    @GetMapping("/logout")
    public String logout(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.setAttribute("user", null);
        return "login";
    }

    @GetMapping("/register")
    public String register() {
        return "register";
    }


    @PostMapping("/user/adduser")
    public String addUser(User request, Map<String, Object> map) {
        User user = userService.getUserByAccount(request.getAccount());
        if (user != null) {
            map.put("msg", "该账号已经存在，请登录");
            return "login";
        }
        user = userService.createAccount(user);
        if (user != null) {
            return "redirect:/content";
        }
        map.put("msg", "注册失败，请联系管理员");
        return "register";
    }


    @GetMapping("/content")
    public String content(HttpServletRequest request, Map<String, Object> paramMap) {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        if (user == null) return "login";
        userService.getContent(user, paramMap);
        return "content";
    }

    @GetMapping("/user/personinfo")
    public String personInfo(HttpServletRequest request, Map<String, Object> map) {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return "login";
        }
        map.put("user", user);
        Long following_num = followMapper.countIFollow(user.getAccount());
        Long follower_num = followMapper.countFollowMe(user.getAccount());
        map.put("myfollowing", following_num);
        map.put("follower", follower_num);
        return "personinfo";
    }

}
